<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


require_once 'Classes/Controllers/Controller.php';
require_once 'Classes/Model/StructureModel.php';
require_once 'Classes/View/Pages/ListBasedPage.php';


class StructureController Extends Controller {

    
        public function processRequest() {
        switch (HTTPRequestModel::getInstance()->extractGETValue('action')){
            case 'AddStructure':
                $this->addStructure();
                break;
            case 'EditStructure':
                $this->editStructure();
                break;
            case 'DeleteStructure':
                $this->deleteStructure();
            default:
                $this->listStructures();
                break;
        }
    }
    
    public function addStructure(){
        
         if(strtolower(HTTPRequestModel::getInstance()->getRequest()) == "post"){
              if(strtolower(HTTPRequestModel::getInstance()->getRequest()) == "post"){
             $title = HTTPRequestModel::getInstance()->extractPOSTValue('Title');
             $address = HTTPRequestModel::getInstance()->extractPOSTValue('Address');
             
             $error = 0;
             if($title == ""){
                 $error++;
             }
             if($address == ""){
                 $error++;
             }
             
             if($error == 0){
                 
                 $structure = new Structure($title, $address);
               
                 
                 StructureModel::getInstance()->AddStructure($structure);
                 
                 header('Location: /structures/');
             }
          }
     }
          
         
         else if(strtolower(HTTPRequestModel::getInstance()->getRequest()) == "get") {
            require_once 'Classes/View/Pages/FormBasedPage.php';

            $this->view = new FormBasedPage("New ");
            $this->view->addCSS('/Ressources/style.css');
            $familyId = HTTPRequestModel::getInstance()->extractGETValue('structure');
            $this->view->addElement(new Structure());
            $this->view->display();
         }
        
    }
    
    public function deleteStructure(){
        

        if ( strtolower(HTTPRequestModel::getInstance()->getRequest()) == "get"){
            $structureId = HTTPRequestModel::getInstance()->extractGETValue('structure');
            StructureModel::getInstance()->DeleteStructureById($structureId);
            header('Location: /structures/');
        }
    }
    
    public function editStructure(){
        
         if(strtolower(HTTPRequestModel::getInstance()->getRequest()) == "post"){
             $title = HTTPRequestModel::getInstance()->extractPOSTValue('Title');
             $address = HTTPRequestModel::getInstance()->extractPOSTValue('Address');
             
             $error = 0;
             if($title == ""){
                 $error++;
             }
             if($address == ""){
                 $error++;
             }
             
             if($error == 0){
                 $structureId = HTTPRequestModel::getInstance()->extractGETValue('structure');
                 $structure = StructureModel::getInstance()->getStructureById($structureId);
                 $structure->setName($title);
                 $structure->setAddress($address);
                 
                 StructureModel::getInstance()->EditStructure($structure);
                 
                
             }
              header('Location: /structures/');
         }
         else if(strtolower(HTTPRequestModel::getInstance()->getRequest()) == "get") {
            require_once 'Classes/View/Pages/FormBasedPage.php';
            
            
            $this->view = new FormBasedPage("Edit Structure");
            $this->view->addCSS('/Ressources/style.css');
            $structureId = HTTPRequestModel::getInstance()->extractGETValue('structure');
            $structure = StructureModel::getInstance()->getStructureById($structureId);
            $this->view->addElement($structure);
            $this->view->display();
        }
    }
    
    public function listStructures(){
        
        if ( strtolower(HTTPRequestModel::getInstance()->getRequest()) == "get"){
            try{
                $structures = StructureModel::getInstance()->getAllStructures();
            }
            catch (NoObjectInDatabaseException $e){
                $structures = null;
            }
            $page = new ListBasedPage('Structures');
            $page->addCSS('/Ressources/style.css');
            $page->addElement($structures);
            $page->display();
        }
    }
    
}


?>
